# 爬取厦门人才网的个人简历被查询记录，生成csv文件
import asyncio
from asyncio.windows_events import NULL
from requests_html import HTMLSession
import re
import csv

def plotBarFromTbl(tbl):
    if isinstance(tbl,dict):
        with open("sumary.csv", "a", newline='') as f:
            writer = csv.writer(f)
            writer.writerow(["公司", "次数"])
            for i in tbl.keys():
                writer.writerow([i,tbl[i]])
                print(tbl[i],i)
            else:
                return 

session = HTMLSession()
url = "https://www.xmrc.com.cn/net/talent/QueryHistory.aspx"
payload = {}
headers = {
    'Cookie': '_temp=tag=3b18271b84674fcfb7092dee4fdbac45; ASP.NET_SessionId=tez11vire43tbeezhjinrjoi; www.xmrc.com.cn=44186949; __RequestVerificationToken=8ZRsY3eMs63LDvGX1mUmXBeZg38NDTL60PH-TZGSNGvGc1qA_c2Eq9AHKqscCzD1OyjACw2; _tid=9F6FB77D8A12C8DF8B054020C38E78F5; _trail=tid=94581750&tidn=D56A3B75487BF91D&sip=135'
}
response = session.request("GET", url, headers=headers, data=payload)
response.encoding = 'utf-8'
tbl = {}
try:
    response.html.render(keep_page=True)

    async def main():
        obj = NULL
        objselect = NULL
        await response.html.page.waitFor(1000)
        await response.html.page.type(' [name="ctl00$Body$ctl00$UsernameTextBox"]', 'a7458969')
        await response.html.page.type(' [name="ctl00$Body$ctl00$PasswordTextBox"]', '7458969')
        await response.html.page.screenshot({'path': 'result.png'})
        await response.html.page.click(' [name="ctl00$Body$ctl00$LoginButton"]')
        await response.html.page.waitFor(1000)
        await response.html.page.screenshot({'path': 'result2.png'})
        await response.html.page.waitFor(1000)
        objselect = await response.html.page.querySelector('#ctl00_ctl00_Body_BodyPart_Pager > div:nth-child(1)')
        await objselect.screenshot({'path': 'result.png'})
        obj = await objselect.getProperty("innerHTML")
        pagestr = await obj.jsonValue()
        # 第1/13页，每页显示30条，共377条
        # (?<=第1/).\d+(?=页)
        pagestr = re.search('(?<=第1/).\d+(?=页)', pagestr.strip()).group(0)
        count = 0
        for k in range(int(pagestr.strip()) + 1):
            tbody = await response.html.page.querySelector("#container > table:nth-child(4) > tbody > tr > td:nth-child(3) > div:nth-child(1) > table.talentTable > tbody")
            for i in range(30):
                # 按位置顺序依次输出
                alltr = "tr:nth-child(%d) > td:nth-child(2) > a" % (i + 1)
                tableselect = await tbody.querySelector(alltr)
                if tableselect == None:
                    break
                temp = await tableselect.getProperty("innerHTML")
                company_name = await temp.jsonValue()
                print('%d ' % count, company_name.strip())
                if company_name.strip() in tbl.keys():
                    tbl[company_name.strip()] =  tbl[company_name.strip()] + 1
                else:
                    tbl[company_name.strip()] = 1
                count += 1

            await response.html.page.click('#ctl00_ctl00_Body_BodyPart_Pager > div:nth-child(2) > a:nth-child(4)')
            await response.html.page.waitFor(2000)
            # await response.html.page.screenshot({'path': 'result13.png'})
    asyncio.get_event_loop().run_until_complete(main())
    plotBarFromTbl(tbl)
except Exception as e:
    print(e)
finally:
    session.close()
